package com.gemom.gui;

import com.gemom.util.CovMatrixData;
import java.awt.*;
import java.awt.BorderLayout;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JPanel;

import javax.swing.JTabbedPane;
import java.awt.event.*;

import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;

public class Main extends ApplicationFrame {
  
    Page3Panel page3Panel;
    NewJPanel2 page2Panel;
    NewJPanel3 new2Panel;
    JPanel topPanel, 
           panelChartItem, panelnormalDist, panelCovMatrix;
    JButton buttonChartItem,buttonNormalDist,buttonCovMatrix;
    boolean flag1=true;
    ChartItemGUI charitem;
    NormalDistributionItemsGUI normalDistItems;
    CovMatrixGUI covmatrixGUI;
    CovMatrixData covM;
    VarianceSetItemsGUI varSetItemsGUI;
    
    
    public Main(final String title) {
        super(title);
        
        int numberitems=20;
        
        charitem = new ChartItemGUI(numberitems);
        normalDistItems = new NormalDistributionItemsGUI(numberitems);
        covmatrixGUI= new CovMatrixGUI(numberitems,500,330);  
        varSetItemsGUI = new VarianceSetItemsGUI(numberitems);
        
        buttonChartItem = new JButton("Start sampling");
        buttonNormalDist= new JButton("Plot distributions");
        buttonCovMatrix = new JButton("Calculate Variance-Covariance Matrix");
        buttonNormalDist.setEnabled(false);
        buttonCovMatrix.setEnabled(false);
        
        panelChartItem = new JPanel(new BorderLayout());
        panelChartItem.add(BorderLayout.NORTH,charitem);
        panelChartItem.add(BorderLayout.CENTER,buttonChartItem);
        
        panelnormalDist = new JPanel(new BorderLayout());
        panelnormalDist.add(BorderLayout.NORTH,normalDistItems);
        panelnormalDist.add(BorderLayout.CENTER,buttonNormalDist);
        
        panelCovMatrix = new JPanel(new BorderLayout());
        panelCovMatrix.add(BorderLayout.NORTH,covmatrixGUI);
        panelCovMatrix.add(BorderLayout.CENTER,buttonCovMatrix);
                
        topPanel = new JPanel(new GridBagLayout()); 
        GridBagConstraints c = new GridBagConstraints();
        Insets inset=new Insets(5,5,5,5);
	c.insets = inset;
        c.gridwidth = 1;//c.gridheight = 3;
        c.gridx=0;//c.gridy=0;
        topPanel.add(panelChartItem,c);
        c.gridx=1;c.gridy=0;
        topPanel.add(panelnormalDist,c);
        c.gridx=0;c.gridy=1;
        topPanel.add(panelCovMatrix,c);
        c.gridx=1;c.gridy=1;
        topPanel.add(varSetItemsGUI,c);

//////////////////////////////////////////////////////////////////////////////
//second page (or tab) to be used later
       
        page3Panel = new Page3Panel();
        page2Panel = new NewJPanel2(numberitems);
        new2Panel= new NewJPanel3(page2Panel);

///////////////////////////////////////////////////////////////////////////////  
    
        JTabbedPane tabbedPane = new JTabbedPane();
        
        tabbedPane.addTab("   HOME   ",topPanel);
        tabbedPane.setMnemonicAt(0, KeyEvent.VK_1);
        
        tabbedPane.addTab("   TEST2   ",page2Panel);
        tabbedPane.setMnemonicAt(1, KeyEvent.VK_2);
        
        tabbedPane.addTab("   TEST3   ",new2Panel);
        tabbedPane.setMnemonicAt(1, KeyEvent.VK_3);

        tabbedPane.addTab("   TEST4   ",page3Panel);
        tabbedPane.setMnemonicAt(1, KeyEvent.VK_4);
        
        this.getContentPane().add(BorderLayout.NORTH,tabbedPane);//topPanel goes here
        topPanel.setBackground(Color.WHITE);
        tabbedPane.setBackground(Color.WHITE);
        this.getContentPane().setBackground(Color.WHITE);
        this.setSize(1225,767);
        
    ////////////////////////////////////////////////////////////////////
    ////////////////////////////////////////////////////////////////////
        
         buttonChartItem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
    
                if(flag1==true)
            {   charitem.gRdata();flag1=false; 
                buttonChartItem.setText("Stop sampling");
                buttonCovMatrix.setText("Calculate Variance-Covariance Matrix");
                buttonNormalDist.setText("Plot distributions");
                buttonNormalDist.setEnabled(false);
                buttonCovMatrix.setEnabled(false);
                for(int i=0;i<charitem.series.length;i++)charitem.series[i].clear();
                normalDistItems.clearXYSeries();
                covmatrixGUI.resetMatrixlabels();
                
                varSetItemsGUI.clearTextAreaButton.setEnabled(false);
                varSetItemsGUI.calculateVarbutton.setEnabled(false);
                varSetItemsGUI.resetCheckBoxesbutton.setEnabled(false);
                varSetItemsGUI.disableChecBoxes();
                varSetItemsGUI.clearTxtAreaAndCheckBoxes();
                
            }
            else{charitem.delay.stop(); flag1=true; buttonChartItem.setText("Start new sampling");
                 buttonNormalDist.setEnabled(true);
                 buttonCovMatrix.setEnabled(true);
                 
                 covM = new CovMatrixData(charitem.getSeriesData());
                 covM.calculateMeanAndStdDev();
                 
                 
                 varSetItemsGUI.setCovMatrixDATA(covM);
                 varSetItemsGUI.clearTextAreaButton.setEnabled(true);
                 varSetItemsGUI.calculateVarbutton.setEnabled(true);
                 varSetItemsGUI.resetCheckBoxesbutton.setEnabled(true);
                 varSetItemsGUI.enableChecBoxes();
                 varSetItemsGUI.clearTxtAreaAndCheckBoxes();

                 //    new Thread(new Runnable() { public void run() {
                page2Panel.setSeriesInfo(charitem.getSeriesData(),covM);
            
             //               } }).start(); // start the thread
                 
                    
                }
    
    	}});
    
    ///////////////////////////////////////////////////////////////////////////
    ///////////////////////////////////////////////////////////////////////////
                
         buttonNormalDist.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {       
                
                normalDistItems.createChartSeries(covM.getMeanAndStdDev());
                buttonNormalDist.setText("Distribution plotted");
                buttonNormalDist.setEnabled(false);
                        
           }});
               
   ///////////////////////////////////////////////////////////////////////////
    ///////////////////////////////////////////////////////////////////////////     
           
        buttonCovMatrix.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {       
                
                buttonCovMatrix.setText("Var-Cov Matrix Calculated");
                covmatrixGUI.enterCovMatrixData(covM.getCovMatrix());
                buttonCovMatrix.setEnabled(false);
                            
         //     covM.printCovMatrix();         
           }});
                
   ///////////////////////////////////////////////////////////////////////////
    /////////////////////////////////////////////////////////////////////////// 
           
    }//end constructor
    
    public static void main(final String[] args) {

        final Main demo= new Main("Dynamic Data Demo");
       // demo.pack();
        RefineryUtilities.centerFrameOnScreen(demo);
        demo.setVisible(true);

    }

}























